import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC, expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.service import Service
#编写自动化测试脚本的步骤
#1.打开浏览器--驱动管理打开
#2.输入百度网站，一定要完整的网址，www.baidu.com不行
#3.找到百度输入框，输入内容
#4.点击百度一下按钮
#5.关闭浏览器

# #安装edge浏览器驱动 ，把路径保存下来
# #通过驱动管理去创建edge浏览器的驱动路径
# EdgeIns = EdgeChromiumDriverManager().install()
# #创建edge浏览器驱动对象
# driver = webdriver.Edge(service=Service(EdgeIns))
#2.输入百度网站，一定要完整的网址，www.baidu.com不行
# driver.get("https://www.baidu.com/")
# time.sleep(2)
# #3.找到百度输入框，输入内容
# #By.CSS_SELECTOR By里面有许多查找方式，我们CSS_SELECTOR是通过选择器查找
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# time.sleep(2)
# #4.点击百度一下按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(3)
#5.关闭浏览器


#设置窗口的大小
#窗口的最大化
# driver.maximize_window()
# time.sleep(2)
# #窗口的最小化
# driver.minimize_window()
# time.sleep(2)
# #窗口的全屏
# driver.fullscreen_window()
# time.sleep(2)
# #手动设置窗口的大小
# driver.set_window_size(1024,1000)
# time.sleep(2)


#设置屏幕截图
# filename = "./images/test.png"
# driver.save_screenshot(filename)
# #这种写法会覆盖掉原来的图片

#高阶写法
# filename="autotest"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
# driver.save_screenshot("./images/"+filename)


#关闭窗口
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(1)").click()
# time.sleep(6)
# #切换窗口
# curheadle = driver.current_window_handle #获取当前页面的句柄
# allheadle = driver.window_handles #获取所有标签页的句柄
# for headle in allheadle:
#     if headle != curheadle: #不等于才把驱动对象指向的标签页切换
#         driver.switch_to.window(headle)
#
# driver.close()
# time.sleep(3)


#隐式等待
# #driver.implicitly_wait(3)
# driver.get("https://www.baidu.com/")
# time.sleep(1) #time.sleep()是强制等待
#
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("金智媛")
# #time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# #time.sleep(3)
# #driver.find_element(By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")
# # time.sleep(3)
#
# # wait = WebDriverWait(driver,3)
# # wait.until(EC.invisibility_of_element((By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))
# wait = WebDriverWait(driver,3)
# wait.until(expected_conditions.invisibility_of_element((By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))

#浏览器导航
# driver.get("https://www.baidu.com/")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,'#kw').send_keys("金秀贤")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,'#su').click()
# time.sleep(3)
#
# #页面后退
# driver.back()
# time.sleep(1)
# #页面前进
# driver.forward()
# time.sleep(1)
#
# #刷新页面
# driver.refresh()
# time.sleep(1)

#安装edge浏览器驱动 ，把路径保存下来
#通过驱动管理去创建edge浏览器的驱动路径
EdgeIns = EdgeChromiumDriverManager().install()
# #创建edge浏览器驱动对象
# driver = webdriver.Edge(service=Service(EdgeIns))
#浏览器参数配置
options = webdriver.EdgeOptions()
#设置无头模式
# options.add_argument("-headless")

#设置页面加载策略
# options.page_load_strategy = 'normal' #等待所有的资源加载完成
# options.page_load_strategy = 'eager' #主体框架记载完成，DOM访问就绪
options.page_load_strategy = 'none' #完全不堵塞
#创建浏览器驱动对象
driver = webdriver.Edge(service=Service(EdgeIns),options= options)
#测试一下浏览器是否真的打开了
driver.get("https://haokan.baidu.com/?sfrom=baidu-top")
# time.sleep(3)
print(driver.title)
print(driver.current_url)
driver.quit()









